package cn.iphone.NPlayer.consumer;

import cn.iphone.NPlayer.domain.MediaFile;
import cn.iphone.NPlayer.service.IMediaFileService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(consumerGroup = "media-push-group",
                        topic = "media-topic",
                        selectorExpression = "push-tag",
                        messageModel = MessageModel.CLUSTERING)
public class MediaConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();
        String s = new String(body, StandardCharsets.UTF_8);
        MediaFile mediaFile = JSONObject.parseObject(s, MediaFile.class);
        mediaFileService.handleFile2m3u8(mediaFile);
    }
}
